#include "CZombieVerde.h"

//=============================================================================
CZombieVerde::CZombieVerde() {
    int opcion=rand() % 1;
    if (opcion==0)
        movHorizontal=true;
    else
        movHorizontal=false;
}
//=============================================================================
void CZombieVerde::ObtenerDireccion(){
    int opcion;
    if (movHorizontal)
        opcion=rand() % 1;
    else
        opcion=rand() % 2+3;

    switch (opcion){
        case 0:
            MoveLeft=true;
            MoveRight=false;
            MoveUp=false;
            MoveDown=false;
            break;
        case 1:
            MoveRight=true;
            MoveLeft=false;
            MoveUp=false;
            MoveDown=false;
            break;
        case 3:
            MoveUp=true;
            MoveRight=false;
            MoveLeft=false;
            MoveDown=false;
            break;
        case 4:
            MoveDown=true;
            MoveRight=false;
            MoveUp=false;
            MoveLeft=false;
            break;
    }
}
//-----------------------------------------------------------------------------
void CZombieVerde::OnLoop() {
		//We're not Moving
	if(MoveLeft == false && MoveRight == false &&
        MoveDown == false && MoveUp == false) {
		StopMove();
	}

	if(MoveLeft) {
		AccelX = -0.25;// -1.5;
		AccelY = 0;// -1.5;
	}else

	if(MoveRight) {
		AccelX = 0.25;// 1.5;
		AccelY = 0;// -1.5;
	}
    else
    if(MoveUp) {
		AccelY = -0.25;// -1.5;
		AccelX = 0;// -1.5;
	}else

	if(MoveDown) {
		AccelY = 0.25;// 1.5;
		AccelX = 0;// -1.5;
	}


	SpeedX += AccelX * CFPS::FPSControl.GetSpeedFactor();
	SpeedY += AccelY * CFPS::FPSControl.GetSpeedFactor();

	if(SpeedX > MaxSpeedX)  SpeedX =  MaxSpeedX;
	if(SpeedX < -MaxSpeedX) SpeedX = -MaxSpeedX;
	if(SpeedY > MaxSpeedY)  SpeedY =  MaxSpeedY;
	if(SpeedY < -MaxSpeedY) SpeedY = -MaxSpeedY;

	OnAnimate();
	OnMove(SpeedX, SpeedY);
	//OnMove(5, 5);
}

//-----------------------------------------------------------------------------
void CZombieVerde::OnRender(SDL_Surface* Surf_Display) {
	CEnemigo::OnRender(Surf_Display);
}

//------------------------------------------------------------------------------
void CZombieVerde::OnCleanup() {
	CEnemigo::OnCleanup();
}
//------------------------------------------------------------------------------
void CZombieVerde::OnAnimate() {
	if(SpeedX !=0 || SpeedY!=0) {
		Anim_Control.MaxFrames = 6;
	}else{
		Anim_Control.MaxFrames = 0;
	}

	CEnemigo::OnAnimate();
}
//------------------------------------------------------------------------------
bool CZombieVerde::OnCollision(CObjetoAnimado* Objeto) {
 if (Objeto->Type == ENTITY_TYPE_EXPLOSION ||
     Objeto->Type == ENTITY_TYPE_BALA)
        this->Inactive=true;
    return true;
}
//=============================================================================
void CZombieVerde::OnMove(float MoveX, float MoveY) {
	if(MoveX == 0 && MoveY == 0) return;

	double NewX = 0;
	double NewY = 0;

	//CanJump = false;

	MoveX *= CFPS::FPSControl.GetSpeedFactor();
	MoveY *= CFPS::FPSControl.GetSpeedFactor();

	if(MoveX != 0) {
		if(MoveX >= 0) 	NewX =  CFPS::FPSControl.GetSpeedFactor();
		else 			NewX = -CFPS::FPSControl.GetSpeedFactor();
	}
    else
	if(MoveY != 0) {
		if(MoveY >= 0) 	NewY =  CFPS::FPSControl.GetSpeedFactor();
		else 			NewY = -CFPS::FPSControl.GetSpeedFactor();
	}

	while(true) {
		if(Flags & ENTITY_FLAG_GHOST) {
			PosValid((int)(X + NewX), (int)(Y + NewY)); //We don't care about collisions, but we need to send events to other entities

			X += NewX;
			Y += NewY;
		}else{
			if(PosValid((int)(X + NewX), (int)(Y)) &&
                abs((X+NewX-posicionInicial.X)/TILE_SIZE)<=6
              ) {
				X += NewX;
			}else{
				SpeedX = (-1)*SpeedX;
				MoveLeft=!MoveLeft;
				MoveRight=!MoveRight;
			}

			if(PosValid((int)(X), (int)(Y + NewY))&&
                abs((Y+NewY-posicionInicial.Y)/TILE_SIZE)<=6
              ) {
				Y += NewY;
			}else{
               /* if(MoveY > 0) {
                    //CanJump = true;
                }*/
				SpeedY = (-1)*SpeedY;
				MoveDown=!MoveDown;
				MoveUp=!MoveUp;
			}
		}

		MoveX += -NewX;
		MoveY += -NewY;

		if(NewX > 0 && MoveX <= 0) NewX = 0;
		if(NewX < 0 && MoveX >= 0) NewX = 0;

		if(NewY > 0 && MoveY <= 0) NewY = 0;
		if(NewY < 0 && MoveY >= 0) NewY = 0;

		if(MoveX == 0) NewX = 0;
		if(MoveY == 0) NewY = 0;

		if(MoveX == 0 && MoveY 	== 0) 	break;
		if(NewX  == 0 && NewY 	== 0) 	break;
	}
}
